package com.sgcc.operate.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.sgcc.common.security.service.TokenService;
import com.sgcc.operate.domain.OActiInfo;
import com.sgcc.operate.domain.OPrizeCost;
import com.sgcc.operate.domain.OPutChannel;
import com.sgcc.operate.mapper.*;
import com.sgcc.system.api.model.LoginUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sgcc.operate.domain.OActiRule;
import com.sgcc.operate.service.IOActiRuleService;

/**
 * 活动规则Service业务层处理
 * 
 * @author sgcc
 * @date 2021-03-25
 */
@Service
public class OActiRuleServiceImpl implements IOActiRuleService 
{
    @Autowired
    private OActiRuleMapper oActiRuleMapper;
    @Autowired
    private OPrizeCostMapper oPrizeCostMapper;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private OActiInfoMapper oActiInfoMapper;
    @Autowired
    private OPutChannelMapper oPutChannelMapper;
    @Autowired
    private OMsgMngMapper oMsgMngMapper;

    /**
     * 查询活动规则
     * 
     * @param id 活动规则ID
     * @return 活动规则
     */
    @Override
    public OActiRule selectOActiRuleById(String id)
    {
        OActiRule oActiRule = oActiRuleMapper.selectOActiRuleById(id);
        if(oActiRule!=null){
            OPrizeCost oPrizeCost = new OPrizeCost();
            oPrizeCost.setActiid(id);
            List<OPrizeCost> list = oPrizeCostMapper.selectOPrizeCostList(oPrizeCost);
            oActiRule.setOprizeCostList(list);
        }

        return oActiRule;
    }

    /**
     * 查询活动规则列表
     * 
     * @param oActiRule 活动规则
     * @return 活动规则
     */
    @Override
    public List<OActiRule> selectOActiRuleList(OActiRule oActiRule)
    {
        return oActiRuleMapper.selectOActiRuleList(oActiRule);
    }

    /**
     * 新增活动规则
     * 
     * @param oActiRule 活动规则
     * @return 结果
     */
    @Override
    public int insertOActiRule(OActiRule oActiRule)
    {
        LoginUser loginUser = tokenService.getLoginUser();
        oActiRule.setCreateDate(new Date());
        oActiRule.setUpdateDate(new Date());
        oActiRule.setCreateBy(loginUser.getUserid());
        oActiRule.setUpdateBy(loginUser.getUserid());
        oActiRule.setDelFlag("0");
        String id= UUID.randomUUID().toString().replaceAll("-", "");
        oActiRule.setId(id);
        oActiRuleMapper.insertOActiRule(oActiRule);
        //先获取奖品列表,并删除
        OActiInfo oActiInfo = oActiInfoMapper.selectOActiInfoById(oActiRule.getActiid());
        OPrizeCost cost = new OPrizeCost();
        cost.setActiid(oActiInfo.getId());
        List<OPrizeCost> list = oPrizeCostMapper.selectOPrizeCostList(cost);
        for (OPrizeCost oPrizeCost : list) {
            oPrizeCostMapper.deleteOPrizeCostById(oPrizeCost.getId());
        }
        //新增奖品
        List<OPrizeCost> oprizeCostList = oActiRule.getOprizeCostList();
        double prizecost = 0;
        double othercost = 0;
        if (oActiRule.getOprizeCostList()!=null) {
            for (OPrizeCost oPrizeCost : oprizeCostList) {
                if ("99".equals(oPrizeCost.getPrizetype())&&(StringUtils.isBlank(oPrizeCost.getPrizename())||StringUtils.isBlank(oPrizeCost.getPrizeamount()))) {
                    continue;
                }else if ("99".equals(oPrizeCost.getPrizetype())&&(StringUtils.isNotBlank(oPrizeCost.getPrizename())&&StringUtils.isNotBlank(oPrizeCost.getPrizeamount()))) {
                    othercost += Double.valueOf(oPrizeCost.getPrizeamount());
                    oPrizeCost.setPrizeamount(oPrizeCost.getPrizeamount());
                }else if ("01".equals(oPrizeCost.getPrizetype())&&StringUtils.isNotBlank(oPrizeCost.getPrizeprice())) {
                    double amount = Double.valueOf(oPrizeCost.getPrizeprice())*Double.valueOf(oPrizeCost.getPrizequant());
                    prizecost += amount;
                    oPrizeCost.setPrizeamount(String.valueOf(amount));
                }
                oPrizeCost.setActiid(oActiInfo.getId());
//                oPrizeCost.preInsert();
                oPrizeCost.setCreateDate(new Date());
                oPrizeCost.setUpdateDate(new Date());
                oPrizeCost.setCreateBy(loginUser.getUserid());
                oPrizeCost.setUpdateBy(loginUser.getUserid());
                oPrizeCost.setDelFlag("0");
                String ids= UUID.randomUUID().toString().replaceAll("-", "");
                oPrizeCost.setId(ids);
                oPrizeCostMapper.insertOPrizeCost(oPrizeCost);
            }
        }

        //更新活动表中  奖品费用信息
        oActiInfo.setPrizecost(String.valueOf(prizecost));
        oActiInfo.setOthercost(String.valueOf(othercost));
        oActiInfo.setActibudget(String.valueOf(prizecost+othercost));
        oActiInfo.setH5Flag(oActiRule.getH5flag());
        // 保存投放渠道
        if (StringUtils.isNotEmpty(oActiRule.getChannelsStr())) {
            oPutChannelMapper.delByActi(oActiInfo.getId());
            String[] chinnels = oActiRule.getChannelsStr().split(",");
            for (String chinnel : chinnels) {
                OPutChannel oPutChannel = new OPutChannel();
                oPutChannel.setPutChannel(chinnel);
                oPutChannel.setActiId(oActiInfo.getId());
                oPutChannel.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                oPutChannelMapper.insertOPutChannel(oPutChannel);
            }

//            oPutChannelMapper.addBatch(oActiRule.getChannelsStr(), oActiInfo.getId());
        }
        oActiInfoMapper.updateOActiInfo(oActiInfo);

        return 1;
    }

    /**
     * 修改活动规则
     * 
     * @param oActiRule 活动规则
     * @return 结果
     */
    @Override
    public int updateOActiRule(OActiRule oActiRule)
    {
        LoginUser loginUser = tokenService.getLoginUser();
        oActiRule.setUpdateDate(new Date());
        oActiRule.setUpdateBy(loginUser.getUserid());
        oActiRule.setDelFlag("0");
        oActiRuleMapper.updateOActiRule(oActiRule);
        //先获取奖品列表,并删除
        OActiInfo oActiInfo = oActiInfoMapper.selectOActiInfoById(oActiRule.getActiid());
        OPrizeCost cost = new OPrizeCost();
        cost.setActiid(oActiInfo.getId());
        List<OPrizeCost> list = oPrizeCostMapper.selectOPrizeCostList(cost);
        for (OPrizeCost oPrizeCost : list) {
            oPrizeCostMapper.deleteOPrizeCostById(oPrizeCost.getId());
        }
        //新增奖品
        List<OPrizeCost> oprizeCostList = oActiRule.getOprizeCostList();
        double prizecost = 0;
        double othercost = 0;
        if (oActiRule.getOprizeCostList()!=null) {
            for (OPrizeCost oPrizeCost : oprizeCostList) {
                if ("99".equals(oPrizeCost.getPrizetype())&&(StringUtils.isBlank(oPrizeCost.getPrizename())||StringUtils.isBlank(oPrizeCost.getPrizeamount()))) {
                    continue;
                }else if ("99".equals(oPrizeCost.getPrizetype())&&(StringUtils.isNotBlank(oPrizeCost.getPrizename())&&StringUtils.isNotBlank(oPrizeCost.getPrizeamount()))) {
                    othercost += Double.valueOf(oPrizeCost.getPrizeamount());
                    oPrizeCost.setPrizeamount(oPrizeCost.getPrizeamount());
                }else if ("01".equals(oPrizeCost.getPrizetype())&&StringUtils.isNotBlank(oPrizeCost.getPrizeprice())) {
                    double amount = Double.valueOf(oPrizeCost.getPrizeprice())*Double.valueOf(oPrizeCost.getPrizequant());
                    prizecost += amount;
                    oPrizeCost.setPrizeamount(String.valueOf(amount));
                }
                oPrizeCost.setActiid(oActiInfo.getId());
//                oPrizeCost.preInsert();
                oPrizeCost.setCreateDate(new Date());
                oPrizeCost.setUpdateDate(new Date());
                oPrizeCost.setCreateBy(loginUser.getUserid());
                oPrizeCost.setUpdateBy(loginUser.getUserid());
                oPrizeCost.setDelFlag("0");
                String ids= UUID.randomUUID().toString().replaceAll("-", "");
                oPrizeCost.setId(ids);
                oPrizeCostMapper.insertOPrizeCost(oPrizeCost);
            }
        }

        //更新活动表中  奖品费用信息
        oActiInfo.setPrizecost(String.valueOf(prizecost));
        oActiInfo.setOthercost(String.valueOf(othercost));
        oActiInfo.setActibudget(String.valueOf(prizecost+othercost));
        oActiInfo.setH5Flag(oActiRule.getH5flag());
        // 保存投放渠道
        if (StringUtils.isNotEmpty(oActiRule.getChannelsStr())) {
            oPutChannelMapper.delByActi(oActiInfo.getId());
            String[] chinnels = oActiRule.getChannelsStr().split(",");
            for (String chinnel : chinnels) {
                OPutChannel oPutChannel = new OPutChannel();
                oPutChannel.setPutChannel(chinnel);
                oPutChannel.setActiId(oActiInfo.getId());
                oPutChannel.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                oPutChannelMapper.insertOPutChannel(oPutChannel);
            }
        }
        oActiInfoMapper.updateOActiInfo(oActiInfo);

        return 1;
    }

    /**
     * 批量删除活动规则
     * 
     * @param ids 需要删除的活动规则ID
     * @return 结果
     */
    @Override
    public int deleteOActiRuleByIds(String[] ids)
    {
        return oActiRuleMapper.deleteOActiRuleByIds(ids);
    }

    /**
     * 删除活动规则信息
     * 
     * @param id 活动规则ID
     * @return 结果
     */
    @Override
    public int deleteOActiRuleById(String id)
    {
        return oActiRuleMapper.deleteOActiRuleById(id);
    }
}
